package com.zbvc.web;

import java.io.IOException;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zbvc.dao.BookDao;
import com.zbvc.dao.impl.BookDaoImpl;
import com.zbvc.entity.Admin;
import com.zbvc.entity.Book;

/**
 * Servlet implementation class BorrowBookServlet
 */
@WebServlet(name = "borrowServlet", urlPatterns = { "/borrowServlet" })
public class BorrowBookServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BorrowBookServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		// 获取请求参数
		String tip = request.getParameter("tip");
		BookDao bookDao = new BookDaoImpl();
		HttpSession session = request.getSession(); // 从session中取出admin用户数据
		Admin admin = (Admin)session.getAttribute("admin");
		if("1".equals(tip)) { // 借阅  向history表中插入一条数据
			// aid  bid  card bookname adminname username begintime endtime status=1
			int bid = Integer.parseInt(request.getParameter("bid"));
			int row = bookDao.borrowBook(bid,admin);
			System.out.println("借书"+row+"本");
			response.sendRedirect("/bookManager/user/user_select.jsp");
		}else {
			//还书  将history表中status字段修为0
			String show = request.getParameter("show");
			int hid = Integer.parseInt(request.getParameter("hid"));
			// 调用backBook(int hid);
			int row = bookDao.backBook(hid);
			System.out.println("归还"+row+"本");
			if("1".equals(show)) {
				response.sendRedirect("/bookManager/user/user_borrow.jsp"); // 用户
			}else {
				response.sendRedirect("/bookManager/admin/admin_borrow.jsp"); // 管理员
			}
			
		}
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
